package top.forlenet.utils;

import com.alibaba.druid.pool.DruidDataSourceFactory;
import org.apache.commons.dbutils.QueryRunner;

import javax.sql.DataSource;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Properties;


public class DruidUtils {
    private static DataSource dataSource = null;
    static {
        Properties properties = new Properties();
        FileInputStream fileInputStream = null;

        try {
            //fileInputStream = new FileInputStream("src/main/java/druid.properties");
            fileInputStream = new FileInputStream("src/main/resources/druid.properties");
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
        try {
            properties.load(fileInputStream);

            //获取连接池对象
            dataSource = DruidDataSourceFactory.createDataSource(properties);

        } catch (IOException e) {
            e.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            try {
                fileInputStream.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    public static QueryRunner getQueryRunner() {
        return new QueryRunner(dataSource);
    }
}